var ADMIN = {
		boxTitleExpand: '',
		boxTitleCollapse: '',
		filter: '*',
		fileURL: '',
		infoURL: '',
		currentPath: '',
		rootPath: '',
		currentFile: '',
		culture: 'vi',
		confirmRemove: 'Are you sure ?',
		errorNotDel: 'Error while trying to delete file. You have not permission.',
		errorNotSelect: 'Please, select file !',
		/*FILEUPLOAD*/
		isError: false,
		image: 'jpeg|jpg|gif|png|bmp',
		swf: 'swf',
		init:function(){
			ADMIN.rootPath = ADMIN.currentPath;
			ADMIN.initUpload();
			ADMIN.settings();
			$('li[data-path="'+ADMIN.currentPath+'"] > span' ).addClass('active');
			ADMIN.initControl();
			ADMIN.getFileFilter(ADMIN.currentPath);
			
			$('#page-content-left > #inner, #page-content-right > #inner').mCustomScrollbar({
				scrollButtons:{
					enable:false
				},
				advanced:{
					updateOnBrowserResize:true, 
					updateOnContentResize:true, 
					autoExpandHorizontalScroll:true 
				}
			});	
			$(window).resize(function(){
				ADMIN.settings();
			});
			
			
		},
		selected: function(){
			if(ADMIN.currentFile == ''){
				alert(ADMIN.errorNotSelect);
				return false;
			}
			if(parent.VI_UPLOAD_CONTROL){
				var selectObj = {
						path: ADMIN.currentPath + '/' + ADMIN.currentFile,
						name: ADMIN.currentFile,
						width: 0,
						height: 0
				};
				$.post(ADMIN.infoURL,{
					  	'path':ADMIN.currentPath + '/' + ADMIN.currentFile
					  },
					  function(data, status){
						if(status=='success'){
							selectObj = $.parseJSON(data);
						}
						var ret = parent.VI_UPLOAD_CONTROL.beforeSelect(selectObj);
						if(ret != null && ret == false ) return false;
						parent.VI_UPLOAD_CONTROL.selectedFile = selectObj;
						parent.VI_UPLOAD_CONTROL.afterSelected(selectObj);
						parent.$.fancybox.close();
					 });
				
			}else{
				alert('Browser does not support. Please, use Chrome or other Browser to support json.');
			}
		},
		initControl:function(){
			$('.upload-control > a.select').click(function(){
				ADMIN.selected();
				return false;
			});
			$('.upload-control > a.upload').click(function(){
				var str = $(this).attr('data-link')+'?path='+ADMIN.currentPath+'&filter='+ADMIN.filter;
				$.fancybox.open({
					href : str,
					type : 'iframe',
					padding : 7,
					title: $(this).attr('title').toString().toUpperCase(),
					width: 300,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				return false;
			});
			$('.upload-control > a.rename').click(function(){
				if(ADMIN.currentFile == '') {
					alert(ADMIN.errorNotSelect);
					return false;
				}
				var str = $(this).attr('data-link')+'?path='+ADMIN.currentPath+'&name='+ADMIN.currentFile;
				$.fancybox.open({
					href : str,
					type : 'iframe',
					padding : 7,
					title: $(this).attr('title').toString().toUpperCase(),
					width: 300,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				return false;
			});
			$('.upload-control > a.reload').click(function(){
				$('#page-content-right > #inner #result').html('');
				ADMIN.getFileFilter(ADMIN.currentPath);
				return false;
			});
			$('.upload-control > a.remove').click(function(){
				if(ADMIN.currentFile == '') {
					alert(ADMIN.errorNotSelect);
					return false;
				}
				if(confirm(ADMIN.confirmRemove)){
					$.post($(this).attr('data-link'),{
						  'path':ADMIN.currentPath,
						  'name': ADMIN.currentFile
					  },
					  function(data, status){
						 if(status=='success'){
							var rs = $.parseJSON(data);
							if(rs.status == 'success'){
								ADMIN.getFileFilter(ADMIN.currentPath);
							}else{
								alert(ADMIN.errorNotDel);
							}
						}
					  });
				}
				return false;
			});
			$('.upload-control > a.mkdir').click(function(){
				/*if(ADMIN.currentPath == '') {
					alert(ADMIN.errorNotSelect);
					return false;
				}*/
				var str = $(this).attr('data-link')+'?path='+ADMIN.currentPath;
				$.fancybox.open({
					href : str,
					type : 'iframe',
					padding : 7,
					title: $(this).attr('title').toString().toUpperCase(),
					width: 300,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				return false;
			});
		},
		settings:function(){
			var wd = $('#inner-bound').width()<=100? 100:$('#inner-bound').width();
			wd +=30;
			var hg = $('#page-user-region').height()+30;
			$('#page-content-left > #inner').attr('style','height:'+($(window).height()-hg)+'px;width:'+wd+'px');
			$('#page-content-left').attr('style','width:'+wd+'px');
			$('#page-content-right > #inner').attr('style','height:'+($(window).height()-hg)+'px;width:'+($(window).width()-wd-30)+'px');
		},
		initUpload:function(){
			$(".vi-treeview").treeview({
				animated: "fast",
				collapsed: true,
				unique: true,
				persist: "cookie",
				toggle: function() {
					ADMIN.settings();
				}
			});
			$('.vi-treeview li').click(function(){
				$('.vi-treeview li span').removeClass('active');
				$('> span',this).addClass('active');
				ADMIN.currentPath = $(this).attr('data-path');
				ADMIN.getFileFilter(ADMIN.currentPath);
				return false;
			});
		},
		getFileFilter:function(path){
			$.post(ADMIN.fileURL,{
					  'directory_path':path,
					  'filter':ADMIN.filter,
				  },
				  function(data, status){
					if(status=='success'){
						$('#page-content-right > #inner #result').html('');
						var items = $.parseJSON(data);
						var str = '';
						for(i = 0; i<items.file_list.length;i++){
							var arr = items.file_list[i].toString().split('.');
							if(arr.length<2) continue;
							if(ADMIN.image.toString().indexOf(arr[arr.length-1].toString().toLowerCase())>=0){
								str+='<div class="item-image" title="'+items.file_list[i]+'" alt="'+items.file_list[i]+'" data-path="'+items.file_list[i]+'"><div class="tbl"><div class="img"><img align="absmiddle" src="'+items.path+'/'+items.file_list[i]+'"/></div><div class="name">'+items.file_list[i]+'</div></div></div>';
							}else if(ADMIN.swf.toString().indexOf(arr[arr.length-1].toString().toLowerCase())>=0){ 
								str +='<div class="item-image" title="'+items.file_list[i]+'" alt="'+items.file_list[i]+'" data-path="'+items.file_list[i]+'">'
										+'<div class="tbl">'
											+'<div class="img">'
												+'<div class="not-click" style="width:100px; height:100px;">'
													+'<object type="application/x-shockwave-flash" data="'+items.path+'/'+items.file_list[i]+'" width="100" height="100">'
														+'<param name="movie" value="'+items.path+'/'+items.file_list[i]+'" />'
														+'<param name="AllowScriptAccess" value="always">'
														+'<param name="quality" value="high" />'
														+'<param name="wmode" value="transparent" />'
														+'<param name="swfversion" value="8.0.35.0" />'
														+'<param name="expressinstall" value="/js/ulti/flash-scripts/expressInstall.swf" />'
														+'<embed src="'+items.path+'/'+items.file_list[i]+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" play="true" loop="true" wmode="transparent" allowscriptaccess="always" width="100" height="100">'
													+'</object>'
												+'</div>'
											+'</div><div class="name">'+items.file_list[i]+'</div>'
										+'</div>'
									+'</div>';
							}else{
								str+='<div class="item-image" title="'+items.file_list[i]+'" alt="'+items.file_list[i]+'" data-path="'+items.file_list[i]+'"><div class="tbl"><div class="img"><img align="absmiddle" src="/css/admin/img/global/not-img.png"/></div><div class="name">'+items.file_list[i]+'</div></div></div>';
							}
						}
						$('#page-content-right > #inner #result').html(str);
						$('div.item-image').click(function(){
							$('div.item-image').attr('class','item-image');
							$(this).attr('class','item-image item-image-select');
							ADMIN.currentFile = $(this).attr('data-path');
						});
						$('div.item-image').dblclick(function(){
							ADMIN.selected();
						});
					}
				  });
		},
		getDirectories: function(){
			$.post('/admin/uploadControl/getdir/',{
				  'directory_path':ADMIN.rootPath
			  },
			  function(data, status){
				if(status=='success'){
					$('#page-content-left > #inner #inner-bound').html(data);
					ADMIN.initUpload();
					ADMIN.getFileFilter(ADMIN.rootPath);
				}
			  });
		},
		fileUploadInit:function(){
			if(parseInt(ADMIN.isError,10)==0){
				$('.form-upload .error').hide();
			}
			if(ADMIN.isError == 1){
				parent.ADMIN.getFileFilter(parent.ADMIN.currentPath);
			}
		},
		fileRenameInit:function(){
			if(parseInt(ADMIN.isError,10)==0){
				$('.form-rename .error').hide();
			}
			if(ADMIN.isError == 1){
				parent.$.fancybox.close();
				parent.ADMIN.getFileFilter(parent.ADMIN.currentPath);
			}
		},
		directories:function(){
			if(parseInt(ADMIN.isError,10)==0){
				$('.form-rename .error').hide();
			}
			if(ADMIN.isError == 1){
				parent.$.fancybox.close();
				parent.ADMIN.getDirectories();
			}
		}
}